package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsFlashPromotionApi;
import com.itheima.hmshopping.dubbo.mapper.SmsFlashPromotionMapper;
import com.itheima.hmshopping.entity.SmsFlashPromotion;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsFlashPromotionImpl implements SmsFlashPromotionApi {
   @Autowired
   private SmsFlashPromotionMapper smsFlashPromotionMapper;
    /**
     * 根据活动名称分页查询
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(String keyword, Integer pageNum, Integer pageSize) {
        Page<SmsFlashPromotion> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //模糊查询名字
        lambdaQueryWrapper.like(StrUtil.isNotBlank(keyword),SmsFlashPromotion::getTitle,keyword);
        Page<SmsFlashPromotion> smsFlashPromotionPage = smsFlashPromotionMapper.selectPage(page, lambdaQueryWrapper);
        //返回数据
        return smsFlashPromotionPage;
    }

    /**
     * 添加活动
     * @param smsFlashPromotion
     */
    @Override
    public void create(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.insert(smsFlashPromotion);
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        smsFlashPromotionMapper.updateStatus(Convert.toLong(id),status);

    }

    /**
     * 编辑活动
     * @param id
     * @param promotion
     */
    @Override
    public void update(Integer id, SmsFlashPromotion promotion) {

     smsFlashPromotionMapper.updateById(promotion);

    }

    /**
     * 获取活动详情
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotion getOne(Integer id) {
       return smsFlashPromotionMapper.selectById(Convert.toLong(id));
    }

    /**
     * 删除活动
     * @param id
     */
    @Override
    public void delete(Integer id) {
        smsFlashPromotionMapper.deleteById(Convert.toLong(id));

    }
}
